package jp.co.dailyReport.staff.sql;

import java.util.Calendar;
import java.sql.Date;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.staff.Staff;
import jp.co.dailyReport.Utilities;

/**
 * 社員情報を更新するクラスです。
 */
public class StaffUpdateExecuter implements Executer {

	private static Logger log = Logger.getLogger(StaffUpdateExecuter.class);
	private Staff staff;

	public StaffUpdateExecuter(Staff staff) {
		this.staff = staff;
	}

	/**
	 * 社員IDを基に、社員情報を更新します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return null
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		String sql = "update staff set name=?,usr=?,passwd=?,employmentForm=?, authority=?,targetUnitPrice=?,upDay=? where staffId=?";
		log.debug("sql: " + sql);
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, Utilities.convUniqueCharacter(staff.getName()));
			pstmt.setString(2, Utilities.convUniqueCharacter(staff.getUsr()));
			pstmt.setString(3, Utilities.convUniqueCharacter(staff.getKeyCode()));
			pstmt.setInt(4, staff.getEmploymentSystem());
			pstmt.setInt(5, staff.getPosition());
			pstmt.setInt(6, staff.getTargetUnitPrice());
			Calendar cal = Calendar.getInstance();
			pstmt.setDate(7, new Date(cal.getTime().getTime()));
			pstmt.setInt(8, staff.getStaffId());
			pstmt.execute();
		} finally {
			if(pstmt != null) 
				pstmt.close();
		}
		return null;
	}
}